package com.duowei.dw_pos.sunmiprint;

import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;

import com.duowei.dw_pos.R;
import com.duowei.dw_pos.application.MyApplication;
import com.duowei.dw_pos.bean.DMFKFS;
import com.duowei.dw_pos.bean.Moneys;
import com.duowei.dw_pos.bean.PrintClear;
import com.duowei.dw_pos.bean.WMLSB;
import com.duowei.dw_pos.bean.WMLSBJB;
import com.duowei.dw_pos.bean.Wmslbjb_jiezhang;
import com.duowei.dw_pos.bean.Xsfkfs;
import com.duowei.dw_pos.bean.YunFu;
import com.duowei.dw_pos.tools.DateTimes;
import com.duowei.dw_pos.tools.Users;

import java.math.BigDecimal;
import java.util.List;

import woyou.aidlservice.jiuiv5.ICallback;
import woyou.aidlservice.jiuiv5.IWoyouService;

/**
 * Created by Administrator on 2017-03-27.
 */

public class Prints {

    private final SharedPreferences mPreferences;

    private Prints(){
        mPreferences = MyApplication.getContext().
                getSharedPreferences("user", Context.MODE_PRIVATE);
    }
    private static Prints p;
    public static Prints getPrinter(){
        if(p==null){
            p=new Prints();
        }
        return p;
    }

    private Wmslbjb_jiezhang mWmlsbjb;
    private WMLSB[] mWmlsbs;

    private String printJieZhang="";
    private String printYun="";
    private String mSecond="";

    private IWoyouService woyouService;
    public static ICallback callback = null;
    /**打印列数、每列宽度、每列的对齐方式*/
    private String[] text = new String[3];
    private int[] width = new int[]{20, 6, 8};
    private int[] align = new int[]{0, 0, 0};
    public void bindPrintService(Context context,ServiceConnection connService){
        callback = new ICallback.Stub() {
            @Override
            public void onRunResult(final boolean success) throws RemoteException {
            }
            @Override
            public void onReturnString(final String value) throws RemoteException {
            }
            @Override
            public void onRaiseException(int code, final String msg) throws RemoteException {
            }
        };
        Intent intent = new Intent();
        intent.setPackage("woyou.aidlservice.jiuiv5");
        intent.setAction("woyou.aidlservice.jiuiv5.IWoyouService");
        context.startService(intent);
        context.bindService(intent, connService, Context.BIND_AUTO_CREATE);
    }

    public void setWoyouService(IWoyouService woyouService){
        this.woyouService=woyouService;
    }

    public void setPrintMsg(Wmslbjb_jiezhang wmlsbjb,WMLSB[] wmlsb){
        this.mWmlsbjb=wmlsbjb;
        this.mWmlsbs=wmlsb;
    }

    public void print_yudayin(){
        ThreadPoolManager.getInstance().executeTask(new Runnable() {
            @Override
            public void run() {
                if(woyouService==null){
                    return;
                }
                try {
                    woyouService.setAlignment(1, callback);
                    woyouService.printTextWithFont("桌  号:" + mWmlsbjb.getZH() + "\n", "", 32, callback);
                    woyouService.setAlignment(0, callback);
                    woyouService.printTextWithFont("账单号:" + mWmlsbjb.getWMDBH() + "\n", "", 28, callback);
                    woyouService.printTextWithFont("日  期:" + mWmlsbjb.getJYSJ() + "\n", "", 28, callback);
                    woyouService.printTextWithFont("点单员:" + Users.YHMC + "    人数：" + mWmlsbjb.getJCRS() + "\n", "", 28, callback);
                    woyouService.sendRAWData(BytesUtil.initLine1(384, 1), callback);
                    //_______________________________________________________________________________________________________________________________
                    text[0] = "单品名称";
                    text[1] = "数量";
                    text[2] = "金额";
                    woyouService.printColumnsText(text, width, align, callback);
                    for (int i = 0; i < mWmlsbs.length; i++) {
                        text[0] = mWmlsbs[i].getXMMC();
                        text[1] = mWmlsbs[i].getSL() + "";
                        text[2] = bigDecimal(mWmlsbs[i].getXJ()) + "";
                        woyouService.printColumnsText(text, width, align, callback);
                    }
                    woyouService.sendRAWData(BytesUtil.initLine1(384, 1), callback);
                    //______________________________________________________________________________
                    woyouService.printTextWithFont("原价合计：" + bigDecimal(Moneys.xfzr) + "\n", "", 30, callback);
                    woyouService.printTextWithFont("折    扣：" + bigDecimal(Moneys.zkjr) + "\n", "", 30, callback);
                    woyouService.printTextWithFont("应    付：" + bigDecimal(Moneys.ysjr) + "\n", "", 30, callback);
                    woyouService.sendRAWData(BytesUtil.initLine1(384, 1), callback);
                    woyouService.setAlignment(1, callback);
                    //______________________________________________________________________________
                    woyouService.printTextWithFont("此单据不作结账单使用", "", 32, callback);
                    woyouService.lineWrap(4, callback);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    //现金\支付宝、微信结账
    private int count1=0;
    public void print_jiezhang(final Context context, final float sxje, final float wsje,
                               final String payStyle, final List<DMFKFS>dmfkfsList){
        ThreadPoolManager.getInstance().executeTask(new Runnable() {
            @Override
            public void run() {
                if(woyouService==null){
                    return;
                }
                if(printJieZhang.equals(mWmlsbjb.getWMDBH())
                        &&mPreferences.getBoolean("doubleprint", false)==false){
                    return;
                }else{
                    printJieZhang=mWmlsbjb.getWMDBH();
                }
                try {
                    woyouService.setAlignment(1, callback);// 对齐方式
                    woyouService.printTextWithFont("桌  号:" + mWmlsbjb.getZH() + "\n", "", 32, callback);
                    woyouService.setAlignment(0, callback);
                    woyouService.printTextWithFont("账单号:" + mWmlsbjb.getWMDBH() + "\n", "", 28, callback);
                    woyouService.printTextWithFont("日  期:" + mWmlsbjb.getJYSJ() + "\n", "", 28, callback);
                    woyouService.printTextWithFont("收银员：" + Users.YHMC + "    人数：" + mWmlsbjb.getJCRS() + "\n", "", 28, callback);
                    woyouService.sendRAWData(BytesUtil.initLine1(384, 1), callback);
                    //_______________________________________________________________________________________________________________________________
                    text[0] = "单品名称";
                    text[1] = "数量";
                    text[2] = "金额";
                    woyouService.printColumnsText(text, width, align, callback);
                    for (int i = 0; i < mWmlsbs.length; i++) {
                        text[0] = mWmlsbs[i].getXMMC();
                        text[1] = mWmlsbs[i].getSL() + "";
                        text[2] = bigDecimal(mWmlsbs[i].getXJ()) + "";
                        woyouService.printColumnsText(text, width, align, callback);
                    }
                    woyouService.sendRAWData(BytesUtil.initLine1(384, 1), callback);
                    //_________________________________________________________________________________
                    woyouService.printTextWithFont("原价合计：" + bigDecimal(Moneys.xfzr) + "\n", "", 30, callback);
                    woyouService.printTextWithFont("折    扣：" + bigDecimal(Moneys.zkjr) + "\n", "", 30, callback);
                    woyouService.printTextWithFont("应    付：" + bigDecimal(Moneys.ysjr) + "\n", "", 30, callback);
                    woyouService.sendRAWData(BytesUtil.initLine1(384, 1), callback);
                    //_________________________________________________________________________________
                    for(DMFKFS dmfkfs:dmfkfsList){
                        woyouService.printTextWithFont(dmfkfs.getNr()+"￥"+bigDecimal(dmfkfs.getMoney())+"\n","",28,callback);
                    }
                    woyouService.printTextWithFont("应收现金:￥" + bigDecimal(wsje)+ "\n", "", 30, callback);
                    if(payStyle.equals(context.getString(R.string.payStytle_cash))){
                        woyouService.printTextWithFont(payStyle+":￥"+bigDecimal(sxje)+"  找零:"+bigDecimal(sxje-wsje)+"\n","",30,callback);
                    }else{
                        woyouService.printTextWithFont(payStyle+":￥"+bigDecimal(wsje)+"\n","",30,callback);
                    }
                    woyouService.setAlignment(1, callback);// 对齐方式
                    woyouService.lineWrap(1, callback);
                    woyouService.printTextWithFont("谢谢光临！", "", 30, callback);
                    woyouService.lineWrap(4, callback);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                count1++;
                if(mPreferences.getBoolean("doubleprint", false)&&count1<2){
                    print_jiezhang(context,sxje,wsje,payStyle,dmfkfsList);
                }else{
                    count1=0;
                }
            }
        });
    }

    //云会员结账
    private int count2=0;
    public void print_yun(final Wmslbjb_jiezhang wmlsbjb, final List<WMLSB>wmlsbList, final List<YunFu>listYunFu,
                          final String payStytle, final float other){
        ThreadPoolManager.getInstance().executeTask(new Runnable() {
            @Override
            public void run() {
                if(woyouService==null){
                    return;
                }
                if(printJieZhang.equals(mWmlsbjb.getWMDBH())&&
                        mPreferences.getBoolean("doubleprint", false)==false){
                    return;
                }else{
                    printJieZhang=mWmlsbjb.getWMDBH();
                }
                try {
                    woyouService.setAlignment(1, callback);
                    woyouService.printTextWithFont("桌  号:" +wmlsbjb.getZH() + "\n", "", 32, callback);
                    woyouService.setAlignment(0, callback);
                    woyouService.printTextWithFont("账单号:" + wmlsbjb.getWMDBH() + "\n", "", 28, callback);
                    woyouService.printTextWithFont("日  期:" + DateTimes.getTime2() + "\n", "", 28, callback);
                    woyouService.printTextWithFont("点单员:" + Users.YHMC + "    人数：" + wmlsbjb.getJCRS() + "\n", "", 28, callback);
                    woyouService.sendRAWData(BytesUtil.initLine1(384, 1), callback);
                    //_______________________________________________________________________________________________________________________________
                    text[0] = "单品名称";
                    text[1] = "数量";
                    text[2] = "金额";
                    woyouService.printColumnsText(text, width, align, callback);
                    for (int i = 0; i < wmlsbList.size(); i++) {
                        text[0] = wmlsbList.get(i).getXMMC();
                        text[1] = wmlsbList.get(i).getSL() + "";
                        text[2] = bigDecimal(wmlsbList.get(i).getDJ()*wmlsbList.get(i).getSL()) + "";
                        woyouService.printColumnsText(text, width, align, callback);
                    }
                    woyouService.sendRAWData(BytesUtil.initLine1(384, 1), callback);
                    //_________________________________________________________________________________
                    woyouService.printTextWithFont("原价合计：" + bigDecimal(Moneys.xfzr) + "\n", "", 30, callback);
                    woyouService.printTextWithFont("折    扣：" + bigDecimal(Moneys.xfzr-getZj(wmlsbList)) + "\n", "", 30, callback);
                    woyouService.printTextWithFont("应    付：" + getZj(wmlsbList) + "\n", "", 30, callback);
                    woyouService.sendRAWData(BytesUtil.initLine1(384, 1), callback);
                    //_________________________________________________________________________________
                    for(int i=0;i<listYunFu.size();i++){
                        woyouService.printTextWithFont("云会员—"+listYunFu.get(i).title+"￥"+bigDecimal(listYunFu.get(i).money)+"\n","",28,callback);
                    }
                    if(other>0){
                        woyouService.printTextWithFont(payStytle+"￥"+bigDecimal(other)+"\n","",28,callback);
                    }
                    woyouService.setAlignment(1, callback);// 对齐方式
                    woyouService.lineWrap(1, callback);
                    woyouService.printTextWithFont("谢谢光临！", "", 30, callback);
                    woyouService.lineWrap(4, callback);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                count2++;
                if(mPreferences.getBoolean("doubleprint", false)&&count2<2){
                    print_yun(wmlsbjb,wmlsbList,listYunFu,payStytle,other);
                }else{
                    count2=0;
                }
            }
        });
    }
    //其它付款方式
    private int count3=0;
    public void print_other(final List<DMFKFS>dmfkfsList){
        ThreadPoolManager.getInstance().executeTask(new Runnable() {
            @Override
            public void run() {
                if(woyouService==null){
                    return;
                }
                if(printYun.equals(mWmlsbjb.getWMDBH())&&
                        mPreferences.getBoolean("doubleprint", false)==false){
                    return;
                }else{
                    printYun=mWmlsbjb.getWMDBH();
                }
                try {
                    woyouService.setAlignment(1, callback);
                    woyouService.printTextWithFont("桌  号:" +mWmlsbjb.getZH() + "\n", "", 32, callback);
                    woyouService.setAlignment(0, callback);
                    woyouService.printTextWithFont("账单号:" + mWmlsbjb.getWMDBH() + "\n", "", 28, callback);
                    woyouService.printTextWithFont("日  期:" + DateTimes.getTime2() + "\n", "", 28, callback);
                    woyouService.printTextWithFont("点单员:" + Users.YHMC + "    人数：" + mWmlsbjb.getJCRS() + "\n", "", 28, callback);
                    woyouService.sendRAWData(BytesUtil.initLine1(384, 1), callback);
                    //_______________________________________________________________________________________________________________________________
                    text[0] = "单品名称";
                    text[1] = "数量";
                    text[2] = "金额";
                    woyouService.printColumnsText(text, width, align, callback);
                    for (int i=0;i<mWmlsbs.length;i++) {
                        text[0] = mWmlsbs[i].getXMMC();
                        text[1] = mWmlsbs[i].getSL() + "";
                        text[2] = bigDecimal(mWmlsbs[i].getDJ()*mWmlsbs[i].getSL()) + "";
                        woyouService.printColumnsText(text, width, align, callback);
                    }
                    woyouService.sendRAWData(BytesUtil.initLine1(384, 1), callback);
                    //_________________________________________________________________________________
                    woyouService.printTextWithFont("原价合计：" + bigDecimal(Moneys.xfzr) + "\n", "", 30, callback);
                    woyouService.printTextWithFont("折    扣：" + bigDecimal(Moneys.zkjr) + "\n", "", 30, callback);
                    woyouService.printTextWithFont("应    付：" + bigDecimal(Moneys.ysjr) + "\n", "", 30, callback);
                    woyouService.sendRAWData(BytesUtil.initLine1(384, 1), callback);
                    //_________________________________________________________________________________
                    for(int i=0;i<dmfkfsList.size();i++){
                        woyouService.printTextWithFont(dmfkfsList.get(i).getNr()+"￥"+bigDecimal(dmfkfsList.get(i).getMoney())+"\n","",28,callback);
                    }
                    woyouService.setAlignment(1, callback);// 对齐方式
                    woyouService.lineWrap(1, callback);
                    woyouService.printTextWithFont("谢谢光临！", "", 30, callback);
                    woyouService.lineWrap(4, callback);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                count3++;
                if(mPreferences.getBoolean("doubleprint", false)&&count3<2){
                    print_other(dmfkfsList);
                }else{
                    count3=0;
                }
            }
        });
    }

    //提交订单打印
    public void print_commit(final WMLSBJB wmlsbjb, final List<WMLSB>wmlsbList, final String seconds){
        ThreadPoolManager.getInstance().executeTask(new Runnable() {
            @Override
            public void run() {
                if(woyouService==null){
                    return;
                }
                if(mSecond.equals(seconds)){
                    return;
                }else{
                    mSecond=seconds;
                }
                try {
                    woyouService.setAlignment(1, callback);
                    woyouService.printTextWithFont("桌  号:" +wmlsbjb.getZH() + "\n", "", 32, callback);
                    woyouService.setAlignment(0, callback);
                    woyouService.printTextWithFont("账单号:" + wmlsbjb.getWMDBH() + "\n", "", 28, callback);
                    woyouService.printTextWithFont("日  期:" + DateTimes.getTime2() + "\n", "", 28, callback);
                    woyouService.printTextWithFont("点单员:" + Users.YHMC + "    人数：" + wmlsbjb.getJCRS() + "\n", "", 28, callback);
                    woyouService.sendRAWData(BytesUtil.initLine1(384, 1), callback);
                    //_______________________________________________________________________________________________________________________________
                    text[0] = "单品名称";
                    text[1] = "数量";
                    text[2] = "金额";
                    woyouService.printColumnsText(text, width, align, callback);
                    for (int i = 0; i < wmlsbList.size(); i++) {
                        text[0] = wmlsbList.get(i).getXMMC();
                        text[1] = wmlsbList.get(i).getSL() + "";
                        text[2] = bigDecimal(wmlsbList.get(i).getDJ()*wmlsbList.get(i).getSL()) + "";
                        woyouService.printColumnsText(text, width, align, callback);
                    }
                    woyouService.sendRAWData(BytesUtil.initLine1(384, 1), callback);
                    //______________________________________________________________________________
                    woyouService.printTextWithFont("消费合计：" + getZj(wmlsbList) + "\n", "", 30, callback);
                    woyouService.sendRAWData(BytesUtil.initLine1(384, 1), callback);
                    woyouService.setAlignment(1, callback);
                    woyouService.printTextWithFont("压桌单", "", 30, callback);
                    woyouService.lineWrap(4, callback);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void print_return(final WMLSBJB wmlsbjb, final List<WMLSB>wmlsbList){
        ThreadPoolManager.getInstance().executeTask(new Runnable() {
            @Override
            public void run() {
                if(woyouService==null){
                    return;
                }
                try {
                    woyouService.setAlignment(1, callback);
                    woyouService.printTextWithFont("退品单"+"\n", "", 32, callback);
                    woyouService.setAlignment(0, callback);
                    woyouService.printTextWithFont("账单号:" + wmlsbjb.getWMDBH() + "\n", "", 28, callback);
                    woyouService.printTextWithFont("日  期:" + DateTimes.getTime2() + "\n", "", 28, callback);
                    woyouService.printTextWithFont("点单员:" + Users.YHMC + "    人数：" + wmlsbjb.getJCRS() + "\n", "", 28, callback);
                    woyouService.sendRAWData(BytesUtil.initLine1(384, 1), callback);
                    //_______________________________________________________________________________________________________________________________
                    text[0] = "单品名称";
                    text[1] = "数量";
                    text[2] = "金额";
                    woyouService.printColumnsText(text, width, align, callback);
                    for (int i = 0; i < wmlsbList.size(); i++) {
                        text[0] = wmlsbList.get(i).getXMMC();
                        text[1] = -1*wmlsbList.get(i).getSL() + "";
                        text[2] = bigDecimal(-1*wmlsbList.get(i).getDJ()*wmlsbList.get(i).getSL()) + "";
                        woyouService.printColumnsText(text, width, align, callback);
                    }
                    woyouService.sendRAWData(BytesUtil.initLine1(384, 1), callback);
                    //______________________________________________________________________________
                    woyouService.printTextWithFont("消费合计：" + -1*getZj2(wmlsbList) + "\n", "", 30, callback);
                    woyouService.sendRAWData(BytesUtil.initLine1(384, 1), callback);
                    woyouService.setAlignment(1, callback);
                    woyouService.printTextWithFont("压桌单", "", 30, callback);
                    woyouService.lineWrap(4, callback);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void print_Clear(PrintClear p, List<Xsfkfs>list){
        if(woyouService==null){
            return;
        }
        try {
            woyouService.setAlignment(1, callback);
            woyouService.printTextWithFont("清机下班\n","",32,callback);
            woyouService.setAlignment(0, callback);
            woyouService.printTextWithFont("收银机号："+p.getPad()+"\n","",28,callback);
            woyouService.printTextWithFont("收银员号："+p.getYhmc()+"\n","",28,callback);
            woyouService.printTextWithFont("上班时间："+p.getSbsj()+"\n","",28,callback);
            woyouService.printTextWithFont("下班时间："+p.getXbsj()+"\n","",28,callback);
            woyouService.printTextWithFont("销售笔数："+p.getXsbs()+"\n","",28,callback);
            woyouService.printTextWithFont("原价销售总额：￥"+p.getJezj()+"\n","",28,callback);
            woyouService.printTextWithFont("折扣金额：￥"+p.getZkje()+"\n","",28,callback);
            woyouService.printTextWithFont("应收金额：￥"+p.getYs()+"\n","",28,callback);
            woyouService.printTextWithFont("收现金额：￥"+p.getSs()+"\n","",28,callback);
            woyouService.printTextWithFont("非现金收款:\n+","",28,callback);
            for(int i=0;i<list.size();i++){
                Xsfkfs xsfkfs = list.get(i);
                if(TextUtils.isEmpty(xsfkfs.getDyqzs())||Float.parseFloat(xsfkfs.getDyqzs())<=0){
                   woyouService.printTextWithFont(xsfkfs.getNr() + ":￥" + xsfkfs.getFkje()+"\n","",28,callback);
                }else{
                    woyouService.printTextWithFont(xsfkfs.getNr() + ":￥" + xsfkfs.getFkje()+" 张数:"+ xsfkfs.getDyqzs()+"\n","",28,callback);
                }
            }
            woyouService.lineWrap(4, callback);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    //总计金额
    private float getZj(List<WMLSB>wmlsbList){
        float zj=0f;
        for(WMLSB w:wmlsbList){
            zj=zj+w.getDJ()*w.getSL();
        }
        return bigDecimal2(zj);
    }
    private float getZj2(List<WMLSB>wmlsbList){
        float zj=0f;
        for(WMLSB w:wmlsbList){
            zj=zj+w.getDJ()*w.getSL();
        }
        return bigDecimal(zj);
    }
    public  Float bigDecimal(Float f){
        return BigDecimal.valueOf(f).setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
    }
    private float bigDecimal2(float f){
        float roundMoney = 0;
        if(Users.rounding.equals(MyApplication.getContext().getString(R.string.rounding_jiao))){
            roundMoney=BigDecimal.valueOf(f).setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
        }else if(Users.rounding.equals(MyApplication.getContext().getString(R.string.rounding_yuan))){
            roundMoney=BigDecimal.valueOf(f).setScale(0, BigDecimal.ROUND_HALF_UP).floatValue();
        }
        return roundMoney;
    }
}
